/**
 * @(#)ProxyHostsManager.java 2017年1月22日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 垚丰代理服务器管理
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class ProxyHostsManager {
	public static final Logger LOGGER = LoggerFactory.getLogger(ProxyHostsManager.class);

	private String[] hostArray;

	@Resource
	private RedisClient redisClient;

//	@PostConstruct
	private void init() {
		String hosts = PropertiesUtil.getValue("proxy_hosts");
		hostArray = hosts.split(",");
	}

	public String getHostAndPort(String imsi) {
		int index = 0;
		if (StringUtils.isBlank(imsi) || imsi.equals("0000")) {
			index = (int) (Math.random() * hostArray.length);
		} else {
			index = Math.abs(imsi.hashCode()) % hostArray.length;
		}
		return hostArray[index];
	}

	/**
	 * 
	 * getOssInnerIp:oss内网下载地址
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param imsi
	 * @return
	 */
	public String getOssInnerIp(String imsi) {
		String innerIp = (String) redisClient.get(CacheKey.OSS_INNER_IP);
		if (!StringUtils.isBlank(innerIp)) {
			String[] ips = innerIp.split(",");
			int index = 0;
			if (StringUtils.isBlank(imsi) || imsi.equals("0000")) {
				index = (int) (Math.random() * ips.length);
			} else {
				index = Math.abs(imsi.hashCode()) % ips.length;
			}
			return ips[index];
		}
		return null;
	}

}
